package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;

import com.qianniu.napi.admin.entity.Templatemsg;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.ITemplatemsgService;

import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.helper.HttpHelper;
import com.qianniu.napi.common.util.EncryptUtil;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.bytedeco.javacpp.presets.opencv_core;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>
 * 微信模板消息记录
 * </p>
 *
 * @author chenxuebin
 * @since 2020-09-23
 */
@Controller
public class TemplatemsgController {

    private static final Logger logger = LoggerFactory.getLogger(TemplatemsgController.class);

    @Autowired
    private ITemplatemsgService templatemsgService;

    @RequestMapping("admin/templatemsg/index")
    public String index() {
        return "admin/templatemsg";
    }

    @RequestMapping("/admin/templatemsg/list")
    @ResponseBody
    public Page<Templatemsg> list(@RequestParam(value="limit",required = false) int limit,
                                @RequestParam(value="offset",required = false) int offset) {
        Wrapper<Templatemsg> wrapper = new EntityWrapper<Templatemsg>().eq("mpid",ObjectUtil.getMpid());
        wrapper.orderBy("id",true);
        Page<Templatemsg> page = templatemsgService.selectPage(new Page<Templatemsg>(offset/limit+1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/templatemsg/add")
    public String add(@Valid Templatemsg templatemsg) {

        templatemsg.setMid(ObjectUtil.getMid());
        templatemsg.setMpid(ObjectUtil.getMpid());
        templatemsg.setCreatetime(new Date());

        templatemsg.insert();
        return "admin/templatemsg";
    }

    @RequestMapping("/admin/templatemsg/getTemplatemsgById")
    @ResponseBody
    public Templatemsg getTemplatemsgById(long id) {
        Templatemsg templatemsg = templatemsgService.selectById(id);
        return templatemsg;
    }

    @RequestMapping("/admin/templatemsg/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = templatemsgService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/templatemsg/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Templatemsg templatemsg = new Templatemsg();
        templatemsg.setId(id);
        templatemsgService.updateById(templatemsg);
        return "success";
    }

    @RequestMapping("/admin/templatemsg/modify")
    public String update(Templatemsg templatemsg) {
        boolean isSuccc = templatemsgService.updateById(templatemsg);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

    /// / ************* bellow is templatemsg interface for user *********

    @RequestMapping("/api/templatemsg/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value="status",required = false)String status,
                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if(pageSize==null)pageSize = 10;
        Page page=new Page(pageIndex,pageSize);
        page = templatemsgService.selectPage(page, new EntityWrapper<Templatemsg>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/templatemsg/add")
    public String templatemsgAdd(Templatemsg templatemsg) {

        templatemsg.setCreatetime(new Date());
        if(Strings.isNullOrEmpty(templatemsg.getKeyname4()))templatemsg.setKeysize(3);
        if(Strings.isNullOrEmpty(templatemsg.getKeyname5()))templatemsg.setKeysize(4);
        else templatemsg.setKeysize(5);

        templatemsg.setCreatetime(new Date());
        templatemsg.setMpid(ObjectUtil.getMpid());
        templatemsg.setMid(ObjectUtil.getMid());
        templatemsg.setUid(ObjectUtil.getUid());
        templatemsg.insert();
        return "redirect:index";
    }

    @RequestMapping("/api/templatemsg/modify")
    public String templatemsgUpdate(Templatemsg bean) {
        bean.setModifytime(new Date());
        boolean isSuccc = templatemsgService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

    @RequestMapping("/api/templatemsg/detail")
    @ResponseBody
    public Jinfo getTemplatemsgDetailById(@RequestParam("token") String token,
                                       @RequestParam("id") long id,HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        Templatemsg templatemsg = templatemsgService.selectById(id);
        info.setInfo(templatemsg);
        return info;
    }
}
